package com.baidu.process.chat.parser.sourceHandler.impl;

import com.alibaba.fastjson.JSONObject;
import com.baidu.process.chat.core.action.Actions;
import com.baidu.process.chat.core.web.bean.IMRequestServer;
import com.baidu.process.chat.parser.bean.QueryJsonStr;
import com.baidu.process.chat.parser.sourceHandler.BaseSourceHandler;
import com.baidu.process.config.service.OptAnswerService;
import com.baidu.process.constant.OptEventEnum;
import com.baidu.process.ngd.bean.NGDResBody;
import com.baidu.process.record.bean.DialogueRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.baidu.process.constant.RedisKey.ClarfyKey;

/**
 * @Author:fangyuchao
 * @Description: 澄清处理器
 * @Date: Create in 11:36 2022/11/8
 */
@Service
public class ClarifyHandler implements BaseSourceHandler {
    @Autowired
    private OptAnswerService optAnswerService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public QueryJsonStr handleQuery(NGDResBody ngdResBody, IMRequestServer imRequestServer, DialogueRecord dialogueRecord) {
        QueryJsonStr queryJsonStr = new QueryJsonStr();
        NGDResBody.NGDQueryData  queryData = ngdResBody.getData();
        String sessionId = queryData.getSessionId();
        Map<String,Object> answer = queryData.getAnswer();
        JSONObject clarifyObj = JSONObject.parseObject(JSONObject.toJSONString(answer.get("clarify")));
        JSONObject textObj = JSONObject.parseObject(JSONObject.toJSONString(clarifyObj.get("text")));
        String title = textObj.get("title").toString();
        //将澄清话术用回车拼接
        List<String> list = JSONObject.parseObject(JSONObject.toJSONString(textObj.get("list")), List.class);
        String clarfyListStr =list.stream().collect(Collectors.joining("\n"));
        //获取当前sessionid的澄清次数，连续澄清三次就返回固定话术
        String clarfyNumStr = redisTemplate.opsForValue().get(ClarfyKey+sessionId);
        int clarfyNum = StringUtils.isEmpty(clarfyNumStr)?0:Integer.parseInt(clarfyNumStr);
        //超过三次返回配置话术
        if (clarfyNum>=3){
            queryJsonStr.setAnswer(optAnswerService.getAnswerByEvent(OptEventEnum.clarfyText.name()));
            //重置澄清次数
            redisTemplate.opsForValue().set(ClarfyKey+sessionId,"0",10, TimeUnit.MINUTES);
        }else{
            //没超过三次就累加一次
            redisTemplate.opsForValue().set(ClarfyKey+sessionId,Integer.toString(clarfyNum+1),10,TimeUnit.MINUTES);
            queryJsonStr.setAnswer(title+"\n"+clarfyListStr);
        }
        queryJsonStr.setBot(queryData.getSource());
        queryJsonStr.setSessionId(queryData.getSessionId());
        queryJsonStr.setBot(Actions.DEFAULT.getCode());
        queryJsonStr.setContext(queryData.getContext());
        queryJsonStr.setSource(queryData.getSource());
        return queryJsonStr;
    }
}
